﻿using System;
using System.Drawing;
using System.Windows.Forms;
using WfConnectGL;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace _009纹理映射
{
    public partial class Form1 : Form
    {
        private int[] tex;
        private float x1;
        private float x2;
        private float v1;
        private float v2;
        private IntPtr imgPix;
        private IntPtr imgPix2;
        private int texWidth, texHeight;
        private GLRC glrc;
#if __LINUX__
        private const CallingConvention callcv = CallingConvention.StdCall; 
#else
        private const CallingConvention callcv = CallingConvention.Cdecl;
#endif
        [DllImport("draw.dll",CharSet = CharSet.Auto, CallingConvention = callcv)]
        public static extern void draw(int[] tex, float x1, float x2);

/*        [DllImport("draw.dll", ExactSpelling = false, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void getTex(IntPtr imgPix, IntPtr imgPix2, int width, int height, [Out] int[] tex);*/

        [DllImport("draw.dll", CharSet = CharSet.Auto, CallingConvention = callcv)]
        unsafe public static extern void getTex(byte* imgPix, byte* imgPix2, int width, int height, [Out] int[] tex);

        public Form1()
        {
            InitializeComponent();
            glrc = new GLRC(this.Handle);
            glrc.MakeCurrent();
            x1 = -1.0f;
            x2 = 0.0f;
            v1 = 0.04f;
            v2 = -0.02f;
            //载入图片
            Bitmap image1 = new Bitmap("emoji.jpg");
            Bitmap image2 = new Bitmap("cat.jpg");

            Console.WriteLine("image1.Width {0} image1.Height {1}", image1.Width, image1.Height);
            Console.WriteLine("image2.Width {0} image2.Height {1}", image2.Width, image2.Height);
            //获取像素数据
            BitmapData imgData1 = image1.LockBits(new Rectangle(0, 0, image1.Width, image1.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            texWidth = image1.Width;
            texHeight = image1.Height;
            imgPix = imgData1.Scan0;
            int bytes = Math.Abs(imgData1.Stride) * image1.Height;
            byte[] rgbValues = new byte[bytes];
            IntPtr buffImgPix = Marshal.AllocCoTaskMem(bytes);
            IntPtr buffImgPix2 = Marshal.AllocCoTaskMem(bytes);
            Console.WriteLine("debug alloc");
            Marshal.Copy(imgPix, rgbValues, 0, bytes);
            Marshal.Copy(rgbValues, 0, buffImgPix, bytes);
            Console.WriteLine("debug copy");
            image1.UnlockBits(imgData1);
            BitmapData imgData2 = image2.LockBits(new Rectangle(0, 0, image2.Width, image2.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            imgPix2 = imgData2.Scan0;
            byte[] rgbValues2 = new byte[bytes];
            Marshal.Copy(imgPix2, rgbValues2, 0, bytes);
            Marshal.Copy(rgbValues2, 0, buffImgPix2, bytes);
            Console.WriteLine("debug copy 2");
            image2.UnlockBits(imgData2);
            Console.WriteLine("imgPix address {0} size {1} Stride {2}", (long)imgPix, bytes, imgData1.Stride);
            Console.WriteLine("imgPix2 address {0}", (long)imgPix2);
            Console.WriteLine("imgData1 {0}", imgData1);
            // Marshal.Copy(imgPix2, rgbValues, 0, bytes);
            Console.WriteLine("buffImgPix2 address {0}", (long)buffImgPix2);
            tex = new int[2];
            //getTex(buffImgPix, buffImgPix2, texWidth, texHeight, tex);
            unsafe
            {
                fixed (byte* p = rgbValues, p2 = rgbValues2)
                {
                    getTex(p, p2, texWidth, texHeight, tex);
                }
            }
        }

        private void Draw()
        {
            draw(tex, x1, x2);
            glrc.SwapBuffers();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            Draw();
            if (x1 < -1 || x1 > 0.7) v1 = -v1;
            if (x2 < -1 || x2 > 0.7) v2 = -v2;
            x1 = x1 + v1;
            x2 = x2 + v2;
        }
    }
}
